﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.DAL;

namespace Ket_noi_toi_VPP_bang_mo_hinh_ba_lop
{
    public partial class FormNhomSP : Form
    {
        NhomSP NSP = new NhomSP();

        public FormNhomSP()
        {
            InitializeComponent();
        }

        private void FormNhomSP_Load(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = NSP.SelectAll();
        }
        private void btReset_Click(object sender, EventArgs e)
        {
            this.tbMaNhom.Clear();
            this.tbTenNhom.Clear();
            this.tbMaNhom.Focus();
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            NSP.Insert( tbTenNhom.Text);
            dtgHienThi.DataSource = NSP.SelectAll();
            FormNhomSP_Load(sender,e);
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = NSP.SelectID(Convert.ToInt32(tbMaNhom.Text));
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            NSP.Update(Convert.ToInt32(tbMaNhom.Text), tbTenNhom.Text);
            dtgHienThi.DataSource = NSP.SelectAll();
            FormNhomSP_Load(sender,e);
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgHienThi_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string ID;
            int dong;
            dong = e.RowIndex;
            ID = dtgHienThi.Rows[dong].Cells[1].Value.ToString();
            this.tbMaNhom.Text = dtgHienThi.Rows[dong].Cells[0].Value.ToString();
            this.tbTenNhom.Text = dtgHienThi.Rows[dong].Cells[1].Value.ToString();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            NSP.Delete(Convert.ToInt32(tbMaNhom.Text));
            dtgHienThi.DataSource = NSP.SelectAll();
        }

        private void btGoBack_Click(object sender, EventArgs e)
        {
            FormNhomSP_Load(sender,e);
        }
    }
}
